package com.atguigu.controller;

import com.atguigu.base.BaseController;
import com.atguigu.en.HouseStatus;
import com.atguigu.entity.*;
import com.atguigu.service.*;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Map;

/**
 * Date:2022/11/4
 * Author:ybc
 * Description:
 */
@Controller
@RequestMapping("/house")
public class HouseController extends BaseController {

    @DubboReference
    private CommunityService communityService;
    @DubboReference
    private DictService dictService;
    @DubboReference
    private HouseService houseService;
    @DubboReference
    private HouseImageService houseImageService;
    @DubboReference
    private HouseBrokerService houseBrokerService;
    @DubboReference
    private HouseUserService houseUserService;

    @RequestMapping
    public String findPage(@RequestParam Map<String, Object> filters, Model model){
        saveData2Model(model);
        model.addAttribute("filters", filters);
        //查询房源的分页信息
        PageInfo<House> page = houseService.findPage(filters);
        model.addAttribute("page", page);
        return "house/index";
    }

    @RequestMapping("/create")
    public String create(Model model){
        saveData2Model(model);
        return "house/create";
    }

    @RequestMapping("/save")
    public String save(House house, Model model){
        house.setStatus(HouseStatus.UNPUBLISHED.code);
        houseService.save(house);
        return successPage(model, "新增房源成功");
    }

    @RequestMapping("/edit/{id}")
    public String edit(@PathVariable Long id, Model model){
        //获取要修改的房源信息
        House house = houseService.getById(id);
        model.addAttribute("house", house);
        saveData2Model(model);
        return "house/edit";
    }

    @RequestMapping("/update")
    public String update(House house, Model model){
        houseService.update(house);
        return successPage(model, "修改房源成功");
    }

    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable Long id){
        houseService.delete(id);
        return "redirect:/house";
    }

    @RequestMapping("/publish/{id}/{status}")
    public String updateStatus(@PathVariable Long id, @PathVariable Integer status){
        House house = new House();
        house.setId(id);
        house.setStatus(status);
        houseService.update(house);
        return "redirect:/house";
    }

    @RequestMapping("/{id}")
    public String show(@PathVariable Long id, Model model){
        //查询房源信息
        House house = houseService.getById(id);
        model.addAttribute("house", house);
        //查询房源所对应的小区信息
        Community community = communityService.getById(house.getCommunityId());
        model.addAttribute("community", community);
        //查询房产图片信息
        List<HouseImage> houseImage2List = houseImageService.findHouseImageListByHouseIdAndType(id, 2);
        model.addAttribute("houseImage2List", houseImage2List);
        //查询房源图片信息
        List<HouseImage> houseImage1List = houseImageService.findHouseImageListByHouseIdAndType(id, 1);
        model.addAttribute("houseImage1List", houseImage1List);
        //查询房源所对应的经纪人信息
        List<HouseBroker> houseBrokerList = houseBrokerService.findHouseBrokerListByHouseId(id);
        model.addAttribute("houseBrokerList", houseBrokerList);
        //查询房源所对应的房东信息
        List<HouseUser> houseUserList = houseUserService.findHouseUserListByHouseId(id);
        model.addAttribute("houseUserList", houseUserList);
        return "house/show";
    }

    public void saveData2Model(Model model){
        //获取所有的小区信息
        List<Community> communityList = communityService.findAll();
        //获取所有的户型信息
        List<Dict> houseTypeList = dictService.findDictListByParentDictCode("houseType");
        //获取所有的楼层信息
        List<Dict> floorList = dictService.findDictListByParentDictCode("floor");
        //获取所有的建筑结构信息
        List<Dict> buildStructureList = dictService.findDictListByParentDictCode("buildStructure");
        //获取所有的装修情况信息
        List<Dict> decorationList = dictService.findDictListByParentDictCode("decoration");
        //获取所有的朝向信息
        List<Dict> directionList = dictService.findDictListByParentDictCode("direction");
        //获取所有的房屋用途信息
        List<Dict> houseUseList = dictService.findDictListByParentDictCode("houseUse");
        model.addAttribute("communityList", communityList);
        model.addAttribute("houseTypeList", houseTypeList);
        model.addAttribute("floorList", floorList);
        model.addAttribute("buildStructureList", buildStructureList);
        model.addAttribute("decorationList", decorationList);
        model.addAttribute("directionList", directionList);
        model.addAttribute("houseUseList", houseUseList);
    }

}
